package cn.sail.shiroAuthenticator.security;

import cn.sail.pojo.vo.RespResult;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

public class ShiroFormAuthenticationFilter extends FormAuthenticationFilter {
    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        return super.preHandle(request, response);
    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletResponse resp = (HttpServletResponse) response;
        resp.setContentType("application/json; charset=utf-8");
        RespResult result = new RespResult();
        result.setMessage("你还未登录");
        result.setCode(RespResult.RespCodeTypeEnum.FAILED.getValue());
        String jsonStr = JSON.toJSONString(result);
        response.getWriter().write(jsonStr);
        return false;
    }
}